#include "sendfile.h"

SendFile::SendFile(QTcpSocket* m_tcp, QObject *parent) : QObject(parent)
{
    tcp = m_tcp;
}

void SendFile::sendFile(QString path)
{
    QFile file(path);

    file.open(QFile::ReadOnly);

    QByteArray all;
    all = file.readAll();
    tcp->write(all);

    emit recv();
    emit writeOver();

//    QFile file(path);
//    QFileInfo info(path);
//    int fileSize = info.size();
//    int num = 0;

//    file.open(QFile::ReadOnly);

//    QByteArray line;
//    while(!file.atEnd())
//    {
//        if(num == 0)
//        {
//            tcp->write((char*)&fileSize,4);
//        }
//        line = file.readLine();
//        num += line.size();
//        int percent = (num * 100 / fileSize);
//        emit curPercent(percent);

//        tcp->write(line);
//    }

    //    emit writeOver();
}

void SendFile::receiveFile()
{
    QFile* file = new QFile("revc.txt");
    // 接收数据

    // 判断to do
    file->open(QFile::Append);

    // 读出剩余数据
    QByteArray all = tcp->readAll();
    //QString data = QString::fromUtf8(all);
    //QByteArray all2 = all.toUtf8();

    file->write(all);
    file->close();

}
